package com.ck.express.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.ck.express.db.dao.FailedDeliveryRequestDao
import com.ck.express.db.dao.FailedLatticeRequestDao
import com.ck.express.db.entity.FailedDeliveryRequest
import com.ck.express.db.entity.FailedLatticeRequest

@Database(
    entities = [FailedDeliveryRequest::class, FailedLatticeRequest::class],
    version = 2, // 数据库版本号需要增加
    exportSchema = false
)
abstract class ExpressDatabase : RoomDatabase() {
    abstract fun failedDeliveryRequestDao(): FailedDeliveryRequestDao
    abstract fun failedLatticeRequestDao(): FailedLatticeRequestDao

    companion object {
        private const val DATABASE_NAME = "express_database"
        private var instance: ExpressDatabase? = null

        fun getInstance(context: Context): ExpressDatabase {
            if (instance == null) {
                synchronized(ExpressDatabase::class.java) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        ExpressDatabase::class.java,
                        DATABASE_NAME
                    )
                        .fallbackToDestructiveMigration() // 简化版本更新，实际项目中应使用更安全的方式
                        .build()
                }
            }
            return instance as ExpressDatabase
        }
    }
}